package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.egg.ylt.R;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.DateUtils;
import com.egg.ylt.Utils.GlideRoundTransform;
import com.egg.ylt.pojo.NatatoriumByidEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/9/4.
 */

public class ADA_ServeList extends RecyclerView.Adapter<ADA_ServeList.ServeViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<NatatoriumByidEntity.ServerListBean> mList;
    private OnItemClickListener mItemListener;

    public ADA_ServeList(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    public void setServeList(List<NatatoriumByidEntity.ServerListBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ServeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ServeViewHolder(mInflater.inflate(R.layout.item_shop_serve, parent, false));
    }

    @Override
    public void onBindViewHolder(ServeViewHolder holder, int position) {
        final NatatoriumByidEntity.ServerListBean bean = mList.get(position);

        Glide.with(mContext)
                .load(bean.getImageUrl())
                .transform(new GlideRoundTransform(mContext))
                .transition(new DrawableTransitionOptions().crossFade())
                .placeholder(R.mipmap.ic_serve_default)
                .into(holder.itemServeIvImg);
        holder.itemServeTvName.setText(bean.getName());
        holder.itemServeTvTime.setText("时长" + bean.getServiceTime() + "分钟");
        holder.itemServeTvNewPrice.setText("¥" + DateUtils.getFormattedMoney(bean.getMemberPrice() + ""));
        holder.itemServeTvOldPrice.setText("¥" + DateUtils.getFormattedMoney(bean.getPrice() + ""));
        //设置textView中线
        CustomUtils.setMiddleLine(holder.itemServeTvOldPrice);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemListener.onItemClick(bean.getId(), bean.getServiceTime(), bean.getImageUrl());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ServeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_serve_iv_img)
        ImageView itemServeIvImg;
        @BindView(R.id.item_serve_tv_name)
        TextView itemServeTvName;
        @BindView(R.id.item_serve_tv_time)
        TextView itemServeTvTime;
        @BindView(R.id.item_serve_tv_new_price)
        TextView itemServeTvNewPrice;
        @BindView(R.id.item_serve_tv_xie)
        TextView itemServeTvXie;
        @BindView(R.id.item_serve_tv_old_price)
        TextView itemServeTvOldPrice;

        public ServeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(String id, String serviceTime, String imageUrl);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemListener = listener;
    }
}
